package com.wing.product.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.WarehouseStorageOrder;
import com.wing.product.model.response.WarehouseOutboundOrderVO;
import com.wing.product.service.WarehouseOutboundOrderService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(value = "出库管理", tags = "#/warehouse/product/warehouseOutboundOrder")
@RestController("WarehouseOutboundOrderController")
@RequestMapping("/warehouse/admin/outboundOrder")
@RequiredArgsConstructor
public class WarehouseOutboundOrderController {

    private final WarehouseOutboundOrderService warehouseOutboundOrderService ;

    @ApiOperation(value = "查询所有", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseOutboundOrderVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order,
            String warehouseOutboundOrderCode,
            Long customerId,
            Integer status
    ){
        if(JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)){
            customerId = JwtUtils.getDepartmentId();
        }
        Page<WarehouseStorageOrder> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", order);
        paramMap.put("warehouseOutboundOrderCode", warehouseOutboundOrderCode);
        paramMap.put("customerId", customerId);
        paramMap.put("status", status);

        //搜索
        IPage<WarehouseOutboundOrderVO> list = warehouseOutboundOrderService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "查询出库明细", notes = "查询")
    @GetMapping ("/selectOrderDetail")
    public JsonResult<WarehouseOutboundOrderVO> selectProduct(@RequestParam String id){
        if (!StringUtils.isEmpty(id)){
            WarehouseOutboundOrderVO warehouseOutboundOrderVOList = warehouseOutboundOrderService.selectOrderDetail(id);
            if (ObjectUtil.isNotEmpty(warehouseOutboundOrderVOList)){
                return  JsonResult.success("请求成功",warehouseOutboundOrderVOList);
            }else {
                return  JsonResult.success("数据为空",warehouseOutboundOrderVOList);
            }
        }else {
            return  JsonResult.fail("id为空");
        }
    }

    @ApiOperation(value = "查询出库明细", notes = "查询")
    @GetMapping ("/selectOrderDetailRecord")
    public JsonResult<WarehouseOutboundOrderVO> selectProductRecord(@RequestParam String id){
        if (!StringUtils.isEmpty(id)){
            WarehouseOutboundOrderVO warehouseOutboundOrderVOList = warehouseOutboundOrderService.selectOrderDetailRecord(id);
            if (ObjectUtil.isNotEmpty(warehouseOutboundOrderVOList)){
                return  JsonResult.success("请求成功",warehouseOutboundOrderVOList);
            }else {
                return  JsonResult.success("数据为空",warehouseOutboundOrderVOList);
            }
        }else {
            return  JsonResult.fail("id为空");
        }
    }

    @ApiOperation(value = "添加出库作业", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@RequestBody WarehouseOutboundOrderVO warehouseOutboundOrderVO)  {
        try{
            return warehouseOutboundOrderService.saveWarehouseOutboundOrder(warehouseOutboundOrderVO);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "看板出库趋势", notes = "搜索")
    @GetMapping("/selectOutboundOrderTrend")
    public JsonResult<Map<Object, Object>> selectOutboundOrderTrend() {
        return warehouseOutboundOrderService.selectOutboundOrderTrend();
    }

    @ApiOperation(value = "修改出库订单状态", notes = "修改")
    @GetMapping ("/updateOutboundOrder")
    public JsonResult updateOutboundOrder(@RequestParam String id,@RequestParam String status){
        if (!StringUtils.isEmpty(id)){
            return  warehouseOutboundOrderService.updateOutboundOrder(id,status);
        }else {
            return  JsonResult.fail("id为空");
        }
    }

}
